package com.eqdd.yiqidian.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.SPUtil;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import rx.Observable;
import rx.Subscription;
import rx.schedulers.Schedulers;

/**
 * Created by lvzhihao on 17-3-27.
 */

public class JPushService extends Service {
    private User user;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("JPushService->onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("JPushService->onCreate");
        setJPushAlias();
    }

    private void setJPushAlias() {
        user = GreenDaoUtil.getUser();
        if (user != null) {
            HashSet<String> tags = new HashSet<>();
            tags.add(user.getCompanyId() + "_0");
            tags.add(user.getCompanyId() + "_" + user.getDepartId());
            tags.add(user.getCompanyId() + "__" + user.getPostId());
            JPushInterface.setAlias(getApplicationContext(), 0, user.getGuid());
            JPushInterface.setTags(getApplicationContext(), 0, tags);
        }
        stopSelf();

    }
}
